package operate;

import book.Book;
import book.BookList;
import user.User;

import java.io.*;
import java.util.Scanner;

public class Borrow implements IOperate{
    /**
     * 负责人：
     * 功能：借书
     * 让用户输入他要借的书
     * 从集合中查看如果有此书并未被借出则
     * 将书籍由未借出改为已借出
     * 集合（bookList）同步修改
     * 参数：无
     * 返回类型：void
     */
    @Override
    public void operate(User user,BookList bookList) {
        System.out.println("输入选择借阅的书名：");
        Scanner sc = new Scanner(System.in);
        String bookName = sc.nextLine();
        String bookSelect;
        for (int i = 0; i < bookList.bookLists.size(); i++) {
            bookSelect = bookList.bookLists.get(i).getName();
            if (bookName.equals(bookSelect)) {
                if(bookList.bookLists.get(i).isBorrowed()==false)
                {
                    System.out.println("借阅成功");

                    int p = user.getPoint();//借阅成功积分+1
                    user.setPoint(++p);

                    bookList.bookLists.get(i).setBorrowed(true);
                    Book book =bookList.bookLists.get(i);
                    bookList.HistoryBorrowingBookLists.add(book);//添加到历史借阅集合
                    return;
                }else{
                    System.out.println("已被借阅");
                    return;
                }
            }
        }
        System.out.println("不存在此书");
    }
}